package com.tnl.app.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.tnl.app.entity.UserCoupon;
import com.tnl.base.BaseDAO;


@Repository
public class UserCouponDAO {
	private static final String SQL_GET_ID = "select a.*,b.name,b.money,b.useStartDate,b.useEndDate from tb_user_coupon a left join tb_order_coupon b on a.couponId=b.id  where a.userId = ?";
	private static final String SQL_GET_NAME = "select * from tb_user_coupon  where name = ?";
	private static final String SQL_GET_ALL = "select * from tb_user_coupon ";
	private static final String SQL_GET_COUNT_ID = "select count(*) from tb_user_coupon  where userId = ?";
	
	public List<UserCoupon> getALL() {

		return BaseDAO.queryForList(UserCoupon.class, SQL_GET_ALL);

	}
	
	public List<UserCoupon> getById(int userId,String minOrdersAmount) {
		String sql=SQL_GET_ID;
		if(minOrdersAmount!=null){
			sql+=" and b.minOrdersAmount="+minOrdersAmount+"";
		}
		return  BaseDAO.queryForList(UserCoupon.class, sql, userId);
	}
	public UserCoupon getByName(String name){
		return  BaseDAO.queryForObject(UserCoupon.class, SQL_GET_NAME, name);
	}
	
	public Long getByCountId(int userId){
		return (Long) BaseDAO.queryOneRowOneCol(SQL_GET_COUNT_ID, userId);
	}
	
}
